package fr.demos.cinepantin.controleur;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.xml.wss.saml.assertion.saml11.jaxb10.AuthorityBinding;

import fr.demos.cinepantin.Article;

import fr.demos.cinepantin.dao.DaoArticle;
import fr.demos.cinepantin.dao.interfac.ArticleDao;
import fr.demos.cinepantin.exception.DaoExceptions;


/**
 * @author CQP-DNT
 * Servlet implementation class AfficherArticle
 */
@WebServlet("/AfficherArticle")
public class AfficherArticle extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Inject
	@DaoArticle
	private ArticleDao articledao;

	/**
	 * Default constructor.
	 */
	public AfficherArticle() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		RequestDispatcher rd;

		List<Article> article = new ArrayList<>();
		// récupération des articles en session

		try {
			article = articledao.getAllArticle();
			request.setAttribute("listeArticles", article);

		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("erreurPage", "Article indisponible");

		}

		request.setAttribute("page", "/afficherArticle.jsp");
		rd = request.getRequestDispatcher("/page.jsp");
		rd.forward(request, response);

		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
//		doGet(request, response);
		
		
		String titre = (String) request.getParameter("titre");
		ArrayList<Article> listeArticles = null;
		try {
			listeArticles = (ArrayList<Article>)articledao.getArticleByTitre(titre);
			request.setAttribute("listeArticles", listeArticles);
		} catch (DaoExceptions e) {
			e.printStackTrace();
			request.setAttribute("erreurPage", " Veuillez recommencer plus tard");

		}
		request.setAttribute("page","afficherArticle.jsp");
		request.getRequestDispatcher("page.jsp").forward(request, response);
		
	}

		
	}
